//
//  IsoScene.cpp
//  IsoCplus
//
//  Created by 周张林 on 15/9/15.
//
//

#include "IsoScene.h"

USING_NS_CC;

IsoScene* IsoScene::create(unsigned int size, unsigned int gridX, unsigned int gridZ)
{
    IsoScene *o = new IsoScene();
    if(o&&o->init(size,gridX,gridZ)){
        o->autorelease();
        return o;
    }
    CC_SAFE_DELETE(o);
    return nullptr;
}

bool IsoScene::init(unsigned int size,unsigned int gridX,unsigned int gridZ)
{
    if(!IsoObject::init(size, gridX, gridZ)) return false;
    return true;
}


void IsoScene::setGridData(PathGrid *grid)
{
    _gridData=grid;
}

PathGrid *IsoScene::getGridData() const
{
    return _gridData;
}

void IsoScene::addIsoObject(IsoObject *obj,bool isSort)
{
    addChild(obj,0);
    _sprites.pushBack(obj);
    if(isSort){
        sortIsoObject(obj);
    }
}

void IsoScene::removeIsoObject(IsoObject *obj)
{
    removeChild(obj);
    _sprites.eraseObject(obj);
}

void IsoScene::sortIsoObject(IsoObject *obj)
{
    this->reorderChild(obj, obj->getDepth());
}

void IsoScene::sortAll()
{
    for(auto &sp :_sprites){
        this->sortIsoObject(sp);
        sp->isSort = false;
    }
}

IsoObject* IsoScene::getIsoObjectByNodePos(int nodeX,int nodeZ)
{
    for(auto &sp :_sprites){
        if(sp->getNodeX()==nodeX && sp->getNodeZ()==nodeZ)
        {
            return sp ;
        }
    }
    return nullptr;
}

const Vector<IsoObject *> &IsoScene::getIsoChildren() const
{
    return _sprites;
}

void IsoScene::clearScene()
{
    removeAllChildren();
    _sprites.clear();
}

IsoScene::~IsoScene(){
    _sprites.clear();
    _gridData = nullptr;
}